/**
 *
 */
package com.wedroid.r2d2.support.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wedroid.r2d2.contact.Contact;

/**
 * @author lute
 *
 */
public class GroupResponse extends AbstractBaseResponse {

    private Map<Contact, List<Contact>> groupMembers = new HashMap<>();

    /* (non-Javadoc)
     * @see com.wedroid.r2d2.support.response.AbstractBaseResponse#parseInternal(com.alibaba.fastjson.JSONObject)
     */
    @Override
    protected void parseInternal(JSONObject object) {
        JSONArray contactList = object.getJSONArray("ContactList");
        for (int i = 0; i < contactList.size(); i++) {

            JSONObject groupObject = contactList.getJSONObject(i);
            Contact group = groupObject.toJavaObject(Contact.class);

            String encryChatRoomId = groupObject.getObject("EncryChatRoomId", String.class);

            JSONArray memberList = groupObject.getJSONArray("MemberList");
            List<Contact> members = new ArrayList<Contact>();
            for (int j = 0; j < memberList.size(); j++) {
                Contact member = memberList.getObject(j, Contact.class);
                member.setEncryChatRoomId(encryChatRoomId);
                members.add(member);
            }
            groupMembers.put(group, members);
        }
    }

    /**
     * @return the groupMembers
     */
    public Map<Contact, List<Contact>> getGroupMembers() {
        return Collections.unmodifiableMap(groupMembers);
    }

}
